package com.jiandian.service.impl;

import com.jiandian.consts.DictConsts;
import com.jiandian.entity.Dict;
import com.jiandian.service.ApkService;
import com.jiandian.service.DictService;
import com.jiandian.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ApkServiceImpl implements ApkService {


    @Autowired
    private OssService ossService;
    @Autowired
    private DictService dictService;

    @Override
    public String getApkUrl(String bucketName) {
        Dict byKey = dictService.findByKey(DictConsts.SELF_APK_ADDRESS);
        return byKey.getValue();
    }

    @Override
    public void updateBaseApkUrl(String bucketName, String url) {
        Dict dict = dictService.findByKey(DictConsts.SELF_APK_ADDRESS);
        if (dict == null) {
            dict = new Dict();
            dict.setKey(DictConsts.SELF_APK_ADDRESS);
            dict.setValue(url);
            dictService.add(dict);
        }else{
            dict.setKey(DictConsts.SELF_APK_ADDRESS);
            dict.setValue(url);
            dictService.update(dict);
        }

    }

    @Override
    public String fetchSelfApkUrl(String bucketName) {
        return ossService.getSelfApkUrl(bucketName);
    }

    @Override
    public void syncSelfApkUrl(String bucketName) {
        String s = fetchSelfApkUrl(bucketName);
        updateBaseApkUrl(bucketName, s);
    }
}
